import { createApp } from 'vue'
import { createPinia } from 'pinia'

import App from './App.vue'
import router from './router'
import VeeValidatePlugin from './includes/validation'
import { auth } from './includes/firebase'
import Icon from './directives/icon'
import I18n from './includes/i18n'

import { registerSW } from 'virtual:pwa-register'
import GlobalComponents from './includes/_globals'
import progressBar from './includes/progress-bar'

import './assets/base.css'
import './assets/main.css'
import 'nprogress/nprogress.css'

// 开启离线缓存
registerSW({
  immediate: true, // 每小时检查一次
  onRegistered: (r) => r && setInterval(async () => await r.update(), 3600000),
  // // 注册失败则报错到 console
  onRegisterError: (error) => console.error(error)
})
progressBar(router)

let app
// 状态发生变化时 回调函数执行
auth.onAuthStateChanged(() => {
  if (!app) {
    app = createApp(App)
    app.use(createPinia())
    app.use(router)
    app.use(VeeValidatePlugin)
    app.use(I18n)
    app.directive('icon', Icon)
    app.use(GlobalComponents)

    app.mount('#app')
  }
})
